/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.view.workers;

import java.awt.Image;
import java.util.List;
import java.util.Vector;
import javax.swing.SwingWorker;
import systole.domain.signals.Segment;
import systole.ioHandling.logs.SystoleLogger;
import systole.processor.Slicer;
import systole.view.charts.XYChart;
import systole.view.imageFlow.ImageFlow;
import systole.view.imageFlow.ImageFlowItem;
import systole.view.tabs.controllers.AnalysisController;
import systole.view.tabs.controllers.SelectionItem;

/**
 *
 * @author jmj
 */
public class WorkerSlicer extends SwingWorker<Void, Void> {

    private AnalysisController parentControl;
    private ImageFlow imageFlow;
    private List<ImageFlowItem> items;
    private List<Image> images;
    private Vector<SelectionItem> selectionItems;

    /**
     * @param analysisController
     */
    public WorkerSlicer(AnalysisController analysisController) {
        super();
        this.parentControl = analysisController;
        this.selectionItems = new Vector<SelectionItem>();
    }

    @Override
    protected Void doInBackground() {

        try {
            Slicer slicer = new Slicer();

            this.parentControl.getAnalysis().setAllSegments(slicer.sliceSignal(this.parentControl.getAnalysis().getSignalFiltered()));

            Vector<Segment> sliced = this.parentControl.getAnalysis().getAllSegments().getSegments();

            if ((sliced != null) && (!sliced.isEmpty())) {

                this.items = new Vector<ImageFlowItem>();
                this.images = new Vector<Image>();
                ImageFlowItem flowItem;
                XYChart thumb;
                for (int i = 0; i < sliced.size(); i++) {
                    thumb = new XYChart("Segmento " + (i + 1));
                    thumb.setShowLegend(false);
                    thumb.addSeries("", sliced.elementAt(i).toDoubleArray(), 1);
                    flowItem = new ImageFlowItem(thumb.plotThumbnail(225, 225, false), "Segmento " + (i + 1));
                    this.items.add(flowItem);
                    thumb.setShowTitle(false);
                    this.images.add(thumb.plotThumbnail(200, 150, false));
                    this.selectionItems.add(new SelectionItem(i));
                }
            }
        } catch (Exception e) {
            SystoleLogger.getInstance().logError(e.getMessage());
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void done() {
        this.imageFlow = new ImageFlow(this.items, this.parentControl.getFrmAnalysis().getJpnlSegments().getImagesFlow());
        this.parentControl.getSelectionModel().loadSegments(this.selectionItems);
        this.parentControl.getFrmAnalysis().getJpnlSegments().getImagesFlow().addFlowCharts(this.imageFlow);
        this.parentControl.getFrmAnalysis().getJpnlSegments().getImageMap().loadImageList(this.images);

        this.parentControl.getFrmAnalysis().getAccordionSegments().updatePanel();
        this.parentControl.getFrmAnalysis().getJpnlSegments().showImageMap();

    }
}
